﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Unmanned_MSUI.Areas.API.Filters
{
    public class APIFilterAttribute : Attribute,IActionFilter
    {
        /// <summary>
        /// 执行MVC方法之前
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //验证Session判断用户是否登录
            //var userId = filterContext.HttpContext.Session.GetString("UserId");
            //获取Cookie
            var userIdCookie = filterContext.HttpContext.Request.Cookies["StaffId"];

            //判断是否登录，Session或Cookie为空代表没登录
            if (userIdCookie == null)
            {
                //重定向到登录视图方法
                filterContext.Result = new JsonResult(new { code = 500, msg = "请先登录" });
            }

        }


        /// <summary>
        /// 执行MVC方法之后
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {

        }
    }
}
